package com.example.webchar.webchar.controller;

import com.example.webchar.webchar.pojo.Users;
import com.example.webchar.webchar.service.UserService;
import com.example.webchar.webchar.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * @program: wechat-web-is-my
 * @description
 * @author: 郭逍
 * @create: 2021/8/28 22:21
 **/
@RequestMapping("/api/v1/user")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登陆
     *
     * @param users
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Users users) {
        return Result.success().add("token", userService.toLogin(users));
    }

    /**
     * 注册
     *
     * @param users
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody Users users, @RequestParam String vcode) {
        return !users.getName().equals(null) && !users.getPassword().equals(null) && !vcode.equals(null) ?
                userService.register(users, vcode) : Result.fail().add("err", "请补全所有信息");
        //判断 name && password && vcode 是否为空
    }

    /**
     * 发送验证码
     *
     * @param email 接受验证码的账号
     * @return
     */
    @PostMapping("/sendVCode")
    public Result sendVcode(@RequestParam String email) {
        return email.equals(null) ? Result.fail().add("err", "请输入email信息") : userService.sendVcode(email);

    }

}
